package com.sjf.stream.test;

import com.sjf.stream.pojo.Apple;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author <huangtang>
 * @since 2022/8/11 19:20
 **/
public class Apptest {

    public static List<Apple> appleList = new ArrayList<> (16);
    public static List<String> list = new ArrayList<> (16);

    static {
        appleList.add (new Apple ("red" , 50));
        appleList.add (new Apple ("red" , 50));
        appleList.add (new Apple ("green" , 100));
        appleList.add (new Apple ("green" , 200));
        appleList.add (new Apple ("green" , 300));
    }

    static {
        list.add ("赵丽颖");
        list.add ("张晓梦");
        list.add ("张龙");
        list.add ("刘岩");
        list.add ("岳不群");
    }

    public static void main ( String[] args ) {
        start (() -> System.out.println ("123"));
    }

    public static void start ( myRun run ) {
        new Thread (run).start ();
    }

    @Test
    public void test1 () {
        list.stream ().filter (s -> s.startsWith ("张"))
                .forEach (e -> System.out.println (e));
        System.out.println ("======================");
        list.stream ().filter (e -> e.length () == 3)
                .forEach (e -> System.out.println (e));
        System.out.println ("======================");
        list.stream ().filter (e -> e.startsWith ("张"))
                .filter (e -> e.length () == 3)
                .forEach (e -> System.out.println (e));
    }

    @Test
    public void test2 () {
        list.stream ().forEach (e -> System.out.println (e));
        System.out.println ("======================");
        list.stream ().limit (3)
                .forEach (e -> System.out.println (e));
        System.out.println ("======================");
        list.stream ().skip (3)
                .forEach (e -> System.out.println (e));
        System.out.println ("======================");
        list.stream ().skip (2).limit (2)
                .forEach (e -> System.out.println (e));
    }

    @Test
    public void test3 () {
        Stream<String> s1 = list.stream ().limit (4);
        System.out.println ("======================");
        Stream<String> s2 = list.stream ().skip (2);
        System.out.println ("======================");
        //Stream.concat (s1,s2).forEach (e -> System.out.println (e));
        System.out.println ("======================");
        Stream.concat (s1 , s2).distinct ().forEach (e -> System.out.println (e));
    }

    @Test
    public void test4 () {
        System.out.println (list.stream ().filter (s -> s.startsWith ("张"))
                                    .count ());

    }

    @Test
    public void test5 () {
        List<Apple> collect = appleList.stream ().sorted (Comparator.comparing (Apple::getWeight)).collect (Collectors.toList ());
        System.out.println (collect);
    }

    @Test
    public void test6 () {
        appleList.stream ().filter (e -> e.getWeight () <= 100).distinct ().forEach (e -> System.out.println (e));
    }

    @Test
    public void test7 () {
        List<Apple> collect = appleList.stream ().map (item -> {
            return item;
        }).collect (Collectors.toList ());
        collect.stream ().forEach (e -> System.out.println (e));
    }

    @Test
    public void test8 () {
        System.out.println (appleList.stream ().filter (e -> e.getWeight () >= 100).findAny ());
    }

    @Test
    public void test9 () {
        List<Integer> integers = Arrays.asList (1 , 2 , 3 , 4 , 4 , 5 , 5 , 6 , 7 , 8 , 2 , 2 , 2 , 2);
        Integer reduce = integers.stream ().reduce (0 , ( integer1 , integer2 ) -> integer1 + integer2);
        System.out.println (reduce);
    }

    @Test
    public void test10 () {
        Long count = appleList.stream ().collect (Collectors.counting ());
        long count2 = appleList.stream ().count ();
        System.out.println (count == count2);
    }

    //@Test
    //public void test1(){
    //     appleList.stream ().collect (Collectors.groupingBy (Apple::getColor,Collectors.averagingInt (Apple::getWeight)))
    //             .forEach ((k,v) -> System.out.println (k+":"+v));
    //}

    @Test
    public void test11 () {
        appleList.stream ().collect (Collectors.groupingBy (Apple::getColor , Collectors.groupingBy (Apple::getWeight)))
                .forEach (( k , v ) -> System.out.println (k + ":" + v));
        System.out.println ("============================");
        appleList.stream ().collect (Collectors.groupingBy (Apple::getColor , Collectors.groupingBy (Apple::getWeight , Collectors.counting ())))
                .forEach (( k , v ) -> System.out.println (k + ":" + v));

        HashMap<String, String> map = new HashMap<> ();
        Map<String, String> synchronizedMap = Collections.synchronizedMap (map);
    }

    @Test

    public void test12 () {
        appleList.stream ().filter (e -> "red".equals (e.getColor ()))
                .map (a -> {
                    return a.getColor ();
                }).distinct ()
                .peek (e -> {
                    System.out.println (e);
                })
                .toArray ();
    }

    @FunctionalInterface
    public interface myRun extends Runnable {
        //public void run2();
    }
}
